package com.beone.admin.service.impl;

import java.io.InputStream;
import java.util.Date;
import java.util.List;
import java.util.Optional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.base.SuperServiceImpl;
import com.beone.admin.entity.BaseUser;
import com.beone.admin.entity.ImportDetail;
import com.beone.admin.entity.ImportLog;
import com.beone.admin.helper.ExcelImport;
import com.beone.admin.helper.TaskTemplateHelper;
import com.beone.admin.mapper.ImportDetailMapper;
import com.beone.admin.mapper.ImportLogMapper;
import com.beone.admin.service.ImportLogService;
import com.beone.admin.utils.PaginationGatagridTable;
import com.beone.admin.utils.ServiceUtils;

/**
 * @Title 导入记录流水 服务实现类
 * @Author 秦波
 * @Version 1.0 on 2019-06-10
 * @Copyright 贝旺科权
 */
@Service
public class ImportLogServiceImpl extends SuperServiceImpl<ImportLogMapper, ImportLog> implements ImportLogService {

    @Autowired
    private ImportDetailMapper importDetailMapper;

    /**
     * 分页显示导入记录流水列表
     * 
     * @param entity
     * @param currPage
     *            当前页码
     * @param pageSize
     *            每页显示记录数
     * @return
     */
    @Override
    public PaginationGatagridTable getImportLogPagination(ImportLog entity, int currPage, int pageSize) {
        EntityWrapper<ImportLog> ew = new EntityWrapper<ImportLog>();
        Page<ImportLog> page = new Page<ImportLog>(currPage, pageSize);
        List<ImportLog> rows = baseMapper.selectPage(page, ew);
        return ServiceUtils.createGatagridTableJson(page, rows);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void recordImportLog(BaseUser loginUser, InputStream inputStream) {
        ImportLog log = new ImportLog();
        log.setCreateUser(loginUser.getUserId());
        log.setCreateTime(new Date());
        List<ImportDetail> importDetailList = null;
        try {
            ExcelImport excelImport = TaskTemplateHelper.readExcel(inputStream);
            Optional<ExcelImport> optional = Optional.ofNullable(excelImport);
            Integer deptId = optional.get().getTemplateConfig().getDeptId();
            if (deptId == null) {
                log.setStatus(1);
                log.setRemark("模板缺少配置项，导入失败");
            } else {
                log.setStatus(0);
                log.setDeptId(deptId);
                log.setTemplateId(excelImport.getTemplateConfig().getTemplateId());
                importDetailList = excelImport.getImportDetailList();
            }
        } catch (Exception e) {
            log.setStatus(1);
            log.setRemark("解析出错，导入失败：" + e.getMessage());
        }
        baseMapper.insert(log);

        // 如果是导入成功的日志，则处理排班列表
        if (log.getStatus() == 0 && log.getId() != null) {
            handleAssignTask(log.getId(), importDetailList);
        }
    }

    /**
     * 处理排班
     * 
     * @param logId
     * @param importDetailList
     */
    private void handleAssignTask(Integer logId, List<ImportDetail> importDetailList) {
        if (CollectionUtils.isEmpty(importDetailList)) {
            return;
        }
        importDetailList.forEach(e -> e.setLogId(logId));
        importDetailMapper.batchInsert(importDetailList);
        // TODO 处理排班
    }
}
